﻿using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.U2D;
using UnityEngine.UI;
using Zenject;

public class DifficultySelectUIManager : MonoBehaviourSingleton<DifficultySelectUIManager>
{
    [Inject] private DifficultyService _difficultyService;
    [Inject] private GameManager _gameManager;

    [Header("难度选择GridUI")] [SerializeField]
    private GameObject parent;

    [SerializeField] private GameObject difficultySelectUIPrefab;
    private List<DifficultySelectUI> _difficultySelectUIList = new List<DifficultySelectUI>();
    [Inject] private SpriteAtlas difficultyIcons;


    [Header("难度展示UI")] [SerializeField] private Image difficultyAvatarImage;
    [SerializeField] private TextMeshProUGUI difficultyNameText;
    [SerializeField] private TextMeshProUGUI difficultyDescribeText;


    [Header("武器展示UI")] [SerializeField] private Image weaponAvatarImage;
    [SerializeField] private TextMeshProUGUI weaponNameText;
    [SerializeField] private TextMeshProUGUI isLongText;
    [SerializeField] private TextMeshProUGUI weaponDescribeText;

    [Header("角色展示UI")] [SerializeField] private Image avatarImage;
    [SerializeField] private TextMeshProUGUI roleNameText;
    [SerializeField] private TextMeshProUGUI roleDescribeText;

    [Inject] private DiContainer _container;

    public void UpdateRoleWeaponInfoUI()
    {
        Role currentRole = _gameManager.currentRoleBaseData;
        if (currentRole.unlock)
        {
            //解锁
            roleNameText.text = currentRole.name;
            roleDescribeText.text = currentRole.describe.Replace("\\n", "\n");
            avatarImage.sprite = Resources.Load<Sprite>(currentRole.avatar);
        }
        else
        {
            //未解锁
            roleNameText.text = "？？？";
            roleDescribeText.text = currentRole.unlockConditions;
            avatarImage.sprite = Resources.Load<Sprite>("Image/UI/锁");
        }

        Weapon currentWeapon = _gameManager.currentWeaponsBaseData[0];
        weaponNameText.text = currentWeapon.name;
        weaponDescribeText.text = currentWeapon.describe.Replace("\\n", "\n");
        weaponAvatarImage.sprite = Resources.Load<Sprite>(currentWeapon.avatar);
        if (currentWeapon.isLong)
        {
            isLongText.text = "远程";
        }
        else
        {
            isLongText.text = "近程";
        }
    }

    /**
     * 初始化武器选择的GridUI
     */
    private void Start()
    {
        foreach (Difficulty difficulty in _difficultyService.SelectAllDifficulty())
        {
            GameObject difficultyUI = _container.InstantiatePrefab(difficultySelectUIPrefab, parent.transform);
            difficultyUI.GetComponent<DifficultySelectUI>()?.InitUI(difficulty);
            _difficultySelectUIList.Add(difficultyUI.GetComponent<DifficultySelectUI>());
        }

        _difficultySelectUIList[0].UpdateDifficultyInfoUI();
    }


    public void UpdateDifficultyInfoUI(Difficulty difficulty)
    {
        //解锁
        difficultyNameText.text = difficulty.name;
        if (!string.IsNullOrEmpty(difficulty.describe))
        {
            difficultyDescribeText.text = difficulty.describe.Replace("\\n", "\n");
        }

        difficultyAvatarImage.sprite = difficultyIcons.GetSprite(difficulty.name);
    }


    /**
     * 随机选择一个角色
     */
    public void RandomDifficulty()
    {
        //随机选择一个
        _difficultySelectUIList[Random.Range(0, _difficultySelectUIList.Count)].SelectDifficulty();
    }
}